<div th:fragment="header">
    <div class="header bgff">
        <div class="w-main margin-auto h100 header-main">
            <div class="fr h100 header-link">
                <a href="index.html">首页</a>
                <a href="javascript:void(0)">产品中心</a>
                <a href="javascript:void(0)" class="about-us">关于我们</a>
                <span id="userInfo">
                    <a href="login.html" class="login">登录</a>
                    <a href="register.html" class="register">注册</a>
                </span>

            </div>
        </div>
    </div>
</div>
<script th:inline="javascript" th:fragment="header"  xmlns:th="http://www.thymeleaf.org">
    var userInfo = [[${session.userInfo}]];
    var callback = [[${callBack}]];
     //判断是否已经登录

     function checkLogin() {
     if(null != userInfo) {
     return true;
     }
     return false;
     }
     /**
     * 跳转到登录页面
     */
     function toLogin() {
     window.location.href="/user/login.html?callback=" + encodeURIComponent(document.URL);
     }

    $(function () {
        // 初始化登录数据
        initHeadInfo();
    });
    /**
     * 初始化登录数据
     */
    function initHeadInfo() {
        if(null == userInfo) {
            return;
        }
        var username = userInfo.username;
        var headphoto = userInfo.headphoto;
        var userid = userInfo.userid;
        var html = "";
        if(headphoto){
            html = '<a href="javascript:void(0)" class="fr ml20" onclick="loginOut()">退出登录</a><div class="setting fr"> <img src="'+headphoto+'"><span class="name fr">'+username+'</span></div><span id="userid" style="display: none;">'+userid+'</span>';
        }else{
            html = '<a href="javascript:void(0)" class="fr ml20" onclick="loginOut()">退出登录</a><div class="setting fr"> <img src="../../images/tou.jpg"><span class="name fr">'+username+'</span></div><span id="userid" style="display: none;">'+userid+'</span>';
        }
        $("#userInfo").html(html);
    }

    /**
     * 退出
     */
    function loginOut() {
        $.ajax({
            url : '/user/loginOut.do',
            type : 'GET',
            dataType : 'json',
            success : function(res) {
                var code = res.code;
                if(code=="000000"){
                    layer.msg('退出成功...', {
                        icon: 1,
                        time: 600,
                        shade: 0.2
                    }, function(){
                        location.href = "/user/login.html";
                    });
                } else {
                    layer.msg("注销失败~",{icon:2})
                }
            }
        })
    }
</script>

